package com.petmarket.common;

import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.v3.crypto.keypair.ECDSAKeyPair;
import org.fisco.bcos.sdk.v3.model.CryptoType;

import java.security.KeyPair;


@Slf4j
public  class AccountManger {

    public AccountManger(){
    }


    public static String createAccountKey(){
        // 创建非国密类型的CryptoSuite
        CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
        // 随机生成非国密公私钥对
        CryptoKeyPair cryptoKeyPair = cryptoSuite.generateRandomKeyPair();
        // 获取账户地址
        String accountAddress = cryptoKeyPair.getAddress();

        KeyPair keyPair = cryptoKeyPair.getKeyPair();

        if(log.isInfoEnabled()){
            log.info("AccountManger.createAccountKey:publicKey:{},privateKey:{}",
                    cryptoKeyPair.getHexPublicKey(),cryptoKeyPair.getHexPrivateKey() );
        }

        return cryptoKeyPair.getHexPrivateKey();

        //System.out.println(accountAddress);
    }



    public static CryptoKeyPair loadAccount(String hexPrivateKey){


        // 创建国密类型的KeyFactory
        ECDSAKeyPair keyFacotry = new ECDSAKeyPair();

        // 从十六进制字符串加载hexPrivateKey
        CryptoKeyPair keyPair = keyFacotry.createKeyPair(hexPrivateKey);

        if(log.isInfoEnabled()){
            log.info("AccountManger.createAccountKey:publicKey:{},privateKey:{}",
                    keyPair.getHexPublicKey(),keyPair.getHexPrivateKey() );
        }
        return keyPair;

    }


}
